<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>连接两字母单词得到的最长回文串</title>
</head>
<body>
  <script>
    function fn(words) {
      let hashMap = {}, res = 0;
      for (let i = 0; i < words.length; i++) {
        const reverseWord = words[i][1] + words[i][0]
        if (hashMap[reverseWord]) {
          res += 4
          hashMap[reverseWord] -= 1
        } else {
          hashMap[words[i]] = (hashMap[words[i]] || 0) + 1
        }
      }
      for (let word in hashMap) {
        if (hashMap[word] && word[0] === word[1]) {
          return res + 2
        }
      }
      return res
    }
    console.log(fn(['lc', 'cl', 'gg']))
  </script>
</body>
</html>